
		<?php
        include_once("../submit/pword.php");
        
        set_time_limit ( 0 );
        
		/* Connect to database */
        mysql_connect("$server", "$username", "$password") or die(mysql_error()); 
		mysql_select_db("cs1630History") or die("DB Connect Error: " . mysql_error()); 
		
		/* Array of all files used */
		//$files = array("Baltimore_MD_PT.csv");
		$files = array("Baltimore_MD_PT.csv", "Boston_MA_PT.csv", "Cambridge_MA_PT.csv",
						"Charleston_WV_PT.csv", "Chicago_IL_PT.csv", "Cincinnati_OH_PT.csv",
						"New York_NY_PT.csv");
							
		$pubdate = "2011-07-31"; /* Hard coded for now, as it was same for all files */

		echo "<h2>Intialization Script for Climate Data Files</h2>";
		
        //init_climate_locations();
        
		/* Loop through files array */
		foreach ($files as $filename)
		{
			/* Get city, state from filename */
			$fileparse = explode("_", $filename);
			$city = $fileparse[0];
			
			 /* Get county, state from counties.txt */
			$fcounty = fopen("counties.txt", "r");
			while (!feof($fcounty))
			{
				$line = fgets($fcounty);
				$data = explode(",", $line);
				if (strcmp($data[0], $city) == 0)
				{
					$county = strtoupper($data[1]);
					$state = strtoupper ($data[2]);
					break;
				}
			}
			fclose($fcounty);
			
            $city = strtoupper($city);
            
            echo "County: " . $county . "<br />";
            
			/* Check if location already stored. Add it if not */
			$result = mysql_query("select _id from locations where city = '$city' and state = '$state' and county = '$county'");
			$location = mysql_fetch_array($result);
			if (!$location)
			{
                //this isn't working
                if(strcmp($county,"NULL") == 0)
                {
                
                    mysql_query("insert into locations (country,state,county,city) 
                        values ('UNITED STATES', '$state', NULL, '$city')")
                        or die("MySQL Location Error: " . mysql_error());
                }
                else    
                    mysql_query("insert into locations (country,state,county,city) 
                        values ('UNITED STATES', '$state', '$county', '$city')")
                        or die("MySQL Location Error: " . mysql_error());
			}
			 
			/* Get location ID from table */ 
			 
             $city = strtoupper($city);
             
             echo "City: " . $city . "<br />";
             
             
             $query = "SELECT * FROM locations WHERE locations.city = '$city' AND locations.state = '$state' AND locations.county = '$county'";
             $result = mysql_query($query) or die ("Locations Invalid select " . mysql_error());          
             $array = mysql_fetch_array($result);
			 $location = $array[0];
			 
             echo "Location: " . $location . "<br />";
             
			/* Open the file */
			$fid = fopen("../submit/data/" . $filename,"r");
			if (!$fid)
		 	{
		 		exit("File $filename open failed");
		 	}
			
			/* Debugging display */
			echo "$filename loaded...<br />";
			
			/* Read in titles */
		 	$title = fgets($fid);
		 	$titles = explode(",", $title);
			$data = fgets($fid);
            
			/* Read in data */
		 	while (!feof($fid))
		 	{
                
                
		 		/* Get line of data and split it */
		 		
				$data = rtrim($data);
				$data = str_replace('"','',$data);
				$blocks = explode(",", $data);
				
				/* Set important variables */
				$date = $blocks[6];
				$unit = $blocks[5];
				$value = $blocks[8];
                
				/* Store in database */
				mysql_query("insert into $blocks[4] values 
							(NULL,			
							'$date',
							'$date',
							'$pubdate',
							'DAY',
							1,
							$location,
							4,
							20,
							$value,
							'$unit',
							1,
							'NONE',
							1,
							TRUE,
							FALSE)") or die("MySQL Error: $blocks[4], $date, $unit, $value, $location" . mysql_error());
				
                //get next line
                $data = fgets($fid);
		 	}
			
			/* Affirm success */
			echo "$filename completed!<br />";
		}
		

function init_climate_locations()
{
			/* Get county, state from counties.txt */
			$fcounty = fopen("counties.txt", "r");
            $line = fgets($fcounty);
            
			while (!feof($fcounty))
			{
				$data = explode(",", $line);
                
				$city = strtoupper($data[0]);
				$county = strtoupper($data[1]);
				$state = strtoupper ($data[2]);

                if(strcmp($county, "NULL") == 0)
                    $county == NULL;
                    
                /* Check if location already stored. Add it if not */
                $result = mysql_query("select _id from locations where city = '$city' and state = '$state' and county = '$county'");
                $location = mysql_fetch_array($result);
                if (!$location)
                {
                    mysql_query("insert into locations (country,state,county,city) 
                        values ('UNITED STATES', '$state', '$county', '$city')")
                        or die("MySQL Location Error: " . mysql_error());
                }
            
                $line = fgets($fcounty);
				
            }
			fclose($fcounty);
			
            
            
			
}
        
        
		?>
	</body>
</html>